package com.kusen.village.raise.controller;

import com.kusen.village.raise.entity.Product;
import com.kusen.village.raise.service.ProductService;
import com.kusen.village.raise.util.PageList;
import com.kusen.village.raise.util.R;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @description: 产品控制类
 * @author: ghl
 * @create: 2021-11-25 22:58
 **/
@Slf4j
@RestController
@RequestMapping("/product")
public class ProductController {
    @Autowired
    private ProductService productService;
    List<Product> productList = new ArrayList<>();

    @GetMapping("/getProductToPage")
    public Map<String, Object> getProductToPage(Integer stater, Integer pageSize) {
        if (productList.size() == 0) {
            for (int i = 0; i < 10; i++) {
                productList.add(getBean(i));
            }
        }
        productList=productService.getProductList();
        PageList<Product> pageList=new PageList<>(stater,pageSize);
        productService.getProductListByLimit(pageList);
        Map<String, Object> result = new HashMap<>();
        result.put("result", pageList);
        return R.ok().put("data", result).put("code", 200);
    }

    /**
     * 上传图片到服务器上
     * @return
     */
    @PostMapping("/uploadImage")
    public Map<String,Object> uploadImage(@RequestParam("file") MultipartFile file){
        if(file.isEmpty()){
            return R.ok().put("data","文件为空").put("code",2001);
        }
        log.info("上传图片成功，文件名称为"+file.getOriginalFilename());
        return R.ok().put("data","http://s1.v.360xkw.com/gx/0/hz_211/1637312176181.png").put("code",200);
    }

    /**
     * 添加商品数据
     * @param product
     * @return
     */
    @PostMapping("/addProduct")
    public Map<String,Object> addProduct(Product product){
       int i= productService.saveProduct(product);
       if(i>0){
           return R.ok().put("data","添加成功").put("code",200);
       }
        return R.error().put("data","失败").put("code",20001);
    }

    /**
     * 跟新商品信息
     * @param product
     * @return
     */
    @PostMapping("/updateProduct")
    public Map<String,Object> updateProduct(Product product){
        int i= productService.updateProduct(product);
        if(i>0){
            return R.ok().put("data","修改成功").put("code",200);
        }
        return R.error().put("data","修改失败").put("code",20001);
    }

    /**
     * 删除商品数据
     * @param id
     * @return
     */
    @PostMapping("/deleteProductById")
    public Map<String,Object> deleteProductById(Integer id){
        int i= productService.deleteProductById(id);
        if(i>0){
            return R.ok().put("data","删除成功").put("code",200);
        }
        return R.error().put("data","删除失败").put("code",20001);
    }
    private Product getBean(int i) {
        Product product = new Product();
        product.setId(i);
        product.setProdectName("龚海琳" + i);
        product.setLogo("http://s1.v.360xkw.com/gx/0/hz_211/1637312176181.png");
        return product;
    }
}
